package com.hancher.common.androidutil;

import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2022/2/9 0009 21:41 <br/>
 * 描述 : 字典工具
 */
public class MmkvUtil {

    public static void put(String key, String value) {
        MMKV.defaultMMKV().encode("S_" + key, value);
    }

    public static String getString(String key) {
        return MMKV.defaultMMKV().decodeString("S_" + key, "");
    }

    public static String getString(String key, String def) {
        return MMKV.defaultMMKV().decodeString("S_" + key, def);
    }

    public static void put(String key, int value) {
        MMKV.defaultMMKV().encode("I_" + key, value);
    }

    public static int getInt(String key, int def) {
        return MMKV.defaultMMKV().decodeInt("I_" + key, def);
    }

    public static void put(String key, long value) {
        MMKV.defaultMMKV().encode("L_" + key, value);
    }

    public static long getLong(String key, long def) {
        return MMKV.defaultMMKV().decodeLong("L_" + key, def);
    }

    public static void put(String key, double value) {
        MMKV.defaultMMKV().encode("D_" + key, value);
    }

    public static double getDouble(String key, double def) {
        return MMKV.defaultMMKV().decodeDouble("D_" + key, def);
    }

    public static void put(String key, boolean value) {
        MMKV.defaultMMKV().encode("B_" + key, value);
    }

    public static boolean getBoolean(String key, boolean def) {
        return MMKV.defaultMMKV().decodeBool("B_" + key, def);
    }

    public static boolean getBool(String key, boolean def) {
        return MMKV.defaultMMKV().decodeBool("B_" + key, def);
    }

    public static void put(String key, List<String> value) {
        Set<String> likeSet = new HashSet<>(value);
        MMKV.defaultMMKV().encode("A_" + key, likeSet);
    }

    public static List<String> getList(String key, List<String> def) {
        Set<String> a = MMKV.defaultMMKV().decodeStringSet("A_" + key);
        return new ArrayList<>(a != null ? a : def);
    }

    public static List<String> getList(String key) {
        Set<String> a = MMKV.defaultMMKV().decodeStringSet("A_" + key);
        return new ArrayList<>(a != null ? a : new ArrayList<>());
    }

    public static void put(String key, Parcelable value) {
        MMKV.defaultMMKV().encode("P_" + key, value);
    }

    public static <T extends Parcelable> T getParcelable(String key, Class<T> tClass) {
        return MMKV.defaultMMKV().decodeParcelable("P_" + key, tClass);
    }
}
